<?php

//tailvh@gmail.com

//Require model files
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Product.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productoption.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productoptionvalue.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productoptionvalueoption.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productattribute.php';
//Require objects files
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Product.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Productoptionvalue.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Productoptionvalueoption.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Productattribute.php';
//Require forms files
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Product.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Productoptionvalue.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Productaddinfo.php';



class ProductController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private  $modelProduct;
    private  $modelProductoption;
    private  $modelProductoptionvalue; 
    private  $modelProductoptionvalueoption;
    private  $modelProductattribute;

    private $_redirector;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////

    public function preDispatch() {

    }

    public function init() {
        $layoutPath = APPLICATION_PATH . '/templates/default';
        $option = array('layout' => 'index',
        'layoutPath' => $layoutPath);
        
        Zend_Layout::startMvc($option);
        $this->modelProduct = new End_Models_Product(); 
        $this->modelProductoption = new End_Models_Productoption();
        $this->modelProductoptionvalue = new End_Models_Productoptionvalue();
        $this->modelProductoptionvalueoption = new End_Models_Productoptionvalueoption(); 
        $this->modelProductattribute = new End_Models_Productattribute();
        $this->_redirector = $this->_helper->getHelper('Redirector');
   }

    ///////////////////////
    // U T I L I T I E S //
    ///////////////////////
   
        
    ///////////////////
    // A C T I O N S //
    ///////////////////
    //INDEX ACTION
    public function indexAction() { 
        
        $tmpArrProduct = $this->modelProduct->fetchAll();
        $this->view->arrProduct = $tmpArrProduct; 
    }
    
    public function addinfoAction()
    {
        $id = $this->_getParam('id','0');
        $Products = $this->modelProduct->_find($id);      
        $arrProductOption = $this->modelProductoption->_list();
        $formProductaddinfo = new Application_Form_Productaddinfo();
  
        if (!$this->getRequest()->isGet()) { 

       
            //cancel button in both 2 forms
            $Cancel = $this->getRequest()->getPost('cancel', '0');
            if ($Cancel) {
                $this->_redirector->gotoUrl('product/detail/id/'.$id);
            }
            
            //submit button in form 1
            $Submit = $this->getRequest()->getPost('submit', '0');
            if ($Submit) {
                
                $formData = $this->getRequest()->getPost();
                if ($formProductaddinfo->isValid($formData)) { 
                    
                    $arrProductattribute = $this->modelProductattribute->_filter('product_id = ?', $id);
                    if(isset($arrProductattribute))
                        foreach($arrProductattribute as $Productattribute)
                        {
                            $values[$Productattribute->product_option_id] = $Productattribute->product_option_value_id; 
                        }
                    
                    $options = $formData['product_option_name'];
                    
                    $formProductaddinfoassist = $this->makeAssistform($options, $values);
                    $this->view->form = $formProductaddinfoassist;
                
                } else { 
                    $formData['product_option_name'] = array('0'=>'0');
                    $formProductaddinfoassist = $this->makeAssistform(null);
                    $this->view->form = $formProductaddinfoassist;
                }
            }
            
            //confirm button in form 2
            $Confirm = $this->getRequest()->getPost('confirm', '0');
            if ($Confirm) { 
                
                $id = $this->_getParam('id');
                $Product = $this->modelProduct->_find($id);
                
                if(isset($Product))
                {
                    $arrProductattribute = $this->modelProductattribute->_filter('product_id = ?', $id);
                    if(isset($arrProductattribute))
                    foreach($arrProductattribute as $Productattribute)
                    {
                        $this->modelProductattribute->_delete($Productattribute->product_attribute_id);
                    }
                    
                    $arrFormdata = $this->getRequest()->getPost();
                    if(isset($arrFormdata))
                    foreach($arrFormdata as $key => $Formdata)
                    {  
                        if($key != 'confirm')
                        {
                            $Productattribute = new Productattribute(null, $id, $key, $Formdata, null, null);
                            $this->modelProductattribute->_insert($Productattribute); 
                        }
                    }    
                    $this->_redirector->gotoUrl('product/detail/id/'.$id); 
                }
                $this->view->form = $formProductaddinfoassist;
            }
                    //show form 2
            if(!isset($formData['product_option_name']))
            {
                $formProductaddinfo->product_option_name->addMultiOptions($arrProductOption);
                $this->view->form = $formProductaddinfo; 
            }
        }
        else{
            
            if(isset($Products))                                           
            {
                $arrProductattribute = $this->modelProductattribute->_filter('product_id = ?', $id);
                if(isset($arrProductattribute))
                    foreach($arrProductattribute as $Productattribute)
                    {
                        $arrProductoptionchecked[] = $Productattribute->product_option_id;
                    } 
                
                $UC = new Utility_Converter();                                  
                //$formProductaddinfo->populate($UC->objectToArray($Productoptionvalue));   
                $formProductaddinfo->product_option_name->addMultiOptions($arrProductOption);
                $formProductaddinfo->product_option_name->setValue($arrProductoptionchecked);
           
                $this->view->form = $formProductaddinfo;
            }
            else
            {
                $this->_redirector->gotoUrl('product/detail/id/'.$id);   
            }
        }
        

    }
    
    public function addAction(){ 
        $formProduct = new Application_Form_Product();
  
        if ($this->getRequest()->isGet()) { 
            $formProduct->product_id->setValue($productId);
        } else {
            $Cancel = $this->getRequest()->getPost('cancel', '0');
            if ($Cancel) {
                $this->_redirector->gotoUrl('product/index');
            }
            
            $formData = $this->getRequest()->getPost();
            if ($formProduct->isValid($formData)) { 
                $formProduct->product_image->receive();
                $toDate = date('Y-m-d');
                
                $product = new Product(null, '1', $formData['product_quantity'], $formData['product_model'], $formData['product_name'], 
                                 $formData['product_description'], $formProduct->product_image->getFileName(null, 0), $formData['product_price'], $toDate, $toDate, $formData['product_weight'], '1'/*product_status*/, 
                                 '0'/*product_ordered*/);
               
                $id = $this->modelProduct->_insert($product);

                $this->_redirector->gotoUrl('product/index');
            } else { 
                $formProduct->populate($formData);
            }
        }
        $this->view->form = $formProduct;
    }
    
    public function deleteAction(){
        $id = $this->_getParam('id','0');
        $Product = $this->modelProduct->_find($id);
        if(isset($Product))
        {
            $this->modelProduct->_delete($id);
        }
        
        $arrProductattribute = $this->modelProductattribute->_filter('product_id = ?', $id);
        if(isset($arrProductattribute))
            foreach($arrProductattribute as $Productattribute)
            {
                $this->modelProductattribute->_delete($Productattribute->product_attribute_id);
            }
            
        $this->_redirector->gotoUrl('product/index'); 
    }
    
    public function editAction(){
        $id = $this->_getParam('id','0');
        $Product = $this->modelProduct->_find($id);
        $formProduct = new Application_Form_Product();
        
        if (!$this->getRequest()->isGet())  
        {
            if(isset($Product))
            {               
                $Cancel = $this->getRequest()->getPost('cancel', '0');
                if ($Cancel) {
                    $this->_redirector->gotoUrl('Product/index');
                }

                $formData = $this->getRequest()->getPost();
                if ($formProduct->isValid($formData)) { 
                    $toDate = date('Y-m-d');
                    
                    $Product->product_name          = $formData['product_name'];
                    $Product->product_model         = $formData['product_model'];
                    $Product->product_quantity      = $formData['product_quantity'];
                    $Product->product_description   = $formData['product_description'];
                    $Product->product_price         = $formData['product_price'];
                    $Product->product_weight        = $formData['product_weight'];
                    $Product->product_status        = $formData['product_status'];
                    $Product->product_image         = $formProduct->product_image->getFileName(null, 0);
                    $Product->product_last_modified = $toDate;
                            
                    $id = $this->modelProduct->_update($Product);
                            
                    $this->_redirector->gotoUrl('Product/index');
                } else { 
                    $formProduct->populate($formData);
                }
            }else
            {
                $this->_redirector->gotoUrl('Product/index');
            }    
            $this->view->form = $formProduct;
        }    
        else                                                                    //lần đầu gọi tới ta sẽ gán giá trị của đối tượng lên form
        {       
            if(isset($Product))                                                 //nếu đối tượng tồn tại
            {
                $UC = new Utility_Converter();                                  //tạo công cụ giúp đổi đối tượng thành mảng
                $formProduct->populate($UC->objectToArray($Product));           //đổi đối tượng thành mảng rồi gán cho form vừa tạo
                $this->view->form = $formProduct;
            }
            else
            {
                $this->_redirector->gotoUrl('product/index');   
            }
        }
    }
    
    public function detailAction(){
        $id = $this->_getParam('id','0');
        $Product = $this->modelProduct->_find($id);
        if(isset($Product))
        {
            $arrProductattribute = $this->modelProductattribute->_filter('product_id = ?', $id);
            $i=0;
            if(isset($arrProductattribute))
                foreach($arrProductattribute as $Productattribute)
                {
                    $ProductOption = $this->modelProductoption->_find($Productattribute->product_option_id);
                    $ProductOptionValue = $this->modelProductoptionvalue->_find($Productattribute->product_option_value_id);
                    
                    if(isset($ProductOption) && isset($ProductOptionValue))
                    {
                        $arrOption[] = $ProductOption->product_option_name; 
                        $arrValue[]  = $ProductOptionValue->product_option_value_name;                       
                    }
                }
        }
        $arrProductdetail['option'] = $arrOption;
        $arrProductdetail['value'] = $arrValue;
        
        $this->view->Product = $Product;
        $this->view->arrProductdetail = $arrProductdetail;
    }
    
    
    //more function
    public function makeAssistform($options, $values)
    {
        $formProductaddinfoassist = new Zend_Form();
        $formProductaddinfoassist->setMethod('post');
        
        //production_option_option_value opject
        if(isset($options))
        foreach($options as $option)
        {  
            $Productoption = $this->modelProductoption->_find($option);
            $arrValueid = $this->modelProductoptionvalueoption->_findvalueId($option);
            $arrData = $this->modelProductoptionvalue->_listwithparam($arrValueid);
            
            if(isset($values[$Productoption->product_option_id]))
            {
                $formProductaddinfoassist->addElement('select', $Productoption->product_option_id, array(
                    'label'                    => $Productoption->product_option_name,
                    'multiOptions'             => $arrData,
                    'required'                 => true,
                    'registerInArrayValidator' => false,
                    'value'                    => $values[$Productoption->product_option_id],
                )); 
            }
            else
            { 
                $formProductaddinfoassist->addElement('select', $Productoption->product_option_id, array(
                    'label'                    => $Productoption->product_option_name,
                    'multiOptions'             => $arrData,
                    'required'                 => true,
                    'registerInArrayValidator' => false,
                )); 
            }
        }
        
        $formProductaddinfoassist->addElement('submit', 'confirm', array(
                 'value'                   => 'confirm',
                 'id'                      => 'confirm'
        ));
        $formProductaddinfoassist->confirm->setLabel('Đồng ý');
        
        $formProductaddinfoassist->addElement('submit', 'cancel', array(
                 'value'                   => 'cancel',
                 'id'                      => 'cancel'
        ));
        $formProductaddinfoassist->cancel->setLabel('Hủy bỏ');        
        
        return $formProductaddinfoassist;
    }
}